﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;

namespace Platformer
{
    class FlamePath
    {
        public Texture2D sprite;
        public Vector2 position;
        public Vector2 startPosition;

        private AnimationPlayer player;
        //private AnimationPlayer player;
        Level level;
        private Animation blaze;

        //private Animation fireballCollision;

        public bool collided = false;

        public void LoadContent()
        {          
            //load animation
            blaze = new Animation(level.Content.Load<Texture2D>("Sprites/EnemySkills/blaze"), 0.1f, false);
        }

        public Rectangle rectangle
        {
            get
            {
                int left = (int)position.X;
                int width = sprite.Width;
                int top = (int)position.Y;
                int height = sprite.Height;
                return new Rectangle(left, top, width, height);
            }
        }

        public void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            player.Draw(gameTime, spriteBatch, new Vector2(position.X+30, position.Y +10), SpriteEffects.None);
                spriteBatch.Draw(sprite, position, Color.White);
        }

        public FlamePath(Level l, Vector2 pos, int dir)
        {
            this.level = l;

            position = new Vector2(pos.X, pos.Y);
            startPosition = position;
            if (dir == -1)
            {
                sprite = level.Content.Load<Texture2D>("Sprites/EnemySkills/blank");
            }
            else
            {
                sprite = level.Content.Load<Texture2D>("Sprites/EnemySkills/blank");
            }

            LoadContent();
        }

        public void UpdateFlamePath()
        {
            player.PlayAnimation(blaze);

            //Rectangle the size of the screen so fireballs that
            //fly off screen are deleted.

            Rectangle screenRect = new Rectangle(0, 0, 800, 600);
            if (!screenRect.Contains(new Point(
                (int)position.X,
                (int)position.Y)))
            {
                collided = true;
            }

            //Collision rectangle for each flamePath-Will also be
            //used for collisions with enemies.
            Rectangle flamePathRect = new Rectangle(
                (int)position.X,
                (int)position.Y,
                sprite.Width,
                sprite.Height);

            //Check for collisions with the enemies
            if (flamePathRect.Intersects(level.Player.BoundingRectangle))
            {
                level.Player.currentHP -= 0.5;
                collided = true;
            }



            //Look for adjacent tiles to the fireball
            Rectangle bounds = new Rectangle(
                flamePathRect.Center.X,
                flamePathRect.Center.Y,
                flamePathRect.Width / 4,
                flamePathRect.Height / 4);
            int leftTile = (int)Math.Floor((float)bounds.Left / Tile.Width);
            int rightTile = (int)Math.Ceiling(((float)bounds.Right / Tile.Width)) - 1;
            int topTile = (int)Math.Floor((float)bounds.Top / Tile.Height);
            int bottomTile = (int)Math.Ceiling(((float)bounds.Bottom / Tile.Height)) - 1;

            // For each potentially colliding tile
            for (int y = topTile; y <= bottomTile; ++y)
            {
                for (int x = leftTile; x <= rightTile; ++x)
                {
                    TileCollision collision = level.GetCollision(x, y);

                    //If we collide with an Impassable or Platform tile
                    //then delete our fireball.
                    if (collision == TileCollision.Impassable ||
                        collision == TileCollision.Platform)
                    {
                        if (flamePathRect.Intersects(bounds))
                        {
                            collided = true;
                        }
                    }
                }
            }
        }
        
    }
}

